<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :lg="7" :md="8" :sm="24" :xl="6">
            <a-form-item label="物料代码">
              <a-input placeholder="请输入物料代码" v-model="queryParam.materialCode"></a-input>
            </a-form-item>
          </a-col>
          <a-col :lg="7" :md="8" :sm="24" :xl="6">
            <a-form-item label="物料名称">
              <a-input v-model="queryParam.materialName" />
            </a-form-item>
          </a-col>
          <a-col :lg="7" :md="8" :sm="24" :xl="6">
            <a-form-item label="物料类型">
              <j-multi-select-tag
                dict-code="hf_material_type,material_type_name,id"
                v-model="queryParam.materialType"
              />
            </a-form-item>
          </a-col>
          <a-col :lg="7" :md="8" :sm="24" :xl="6">
            <a-form-item label="工序">
              <j-dict-select-tag dict-code="hf_step,name,id" v-model="queryParam.step" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button icon="search" type="primary" @click="searchQuery">查询</a-button>
      <a-button icon="reload" type="primary" @click="searchReset">重置</a-button>
      <!--      <a-button icon='plus' type='primary' @click='handleAdd'>新增</a-button>-->
      <a-button icon="download" type="primary" @click="handleExportXls('BOM单')">导出</a-button>
      <!--      <a-upload :action='importExcelUrl' :headers='tokenHeader' :multiple='false' :showUploadList='false' name='file'-->
      <!--                @change='handleImportExcel'>-->
      <!--        <a-button icon='import' type='primary'>导入</a-button>-->
      <!--      </a-upload>-->
      <!--      修改物料的发料仓库-->
      <a-button type="primary" @click="() => (warehouseEditModal.visible = true)">修改物料的发料仓库</a-button>
      <a-modal
        :visible="warehouseEditModal.visible"
        :title="warehouseEditModal.title"
        :confirm-loading="warehouseEditModal.confirmLoading"
        @ok="editMaterialWarehouse"
        @cancel="() => (warehouseEditModal.visible = false)"
      >
        <a-form :labelCol="{ span: 4 }" :wrapperCol="{ span: 20 }">
          <a-row>
            <a-col :span="24">
              <a-form-item label="物料类型">
                <j-search-select-tag
                  v-model="warehouseEditModal.materialType"
                  dict="hf_material_type,material_type_name,id"
                  placeholder="请选择物料类型"
                ></j-search-select-tag>
              </a-form-item>
            </a-col>
            <a-col :span="24">
              <a-form-item label="发料仓库">
                <j-search-select-tag
                  v-model="warehouseEditModal.warehouse"
                  dict="hf_warehouse,warehouse_name,id"
                  placeholder="请选择发料仓库"
                ></j-search-select-tag>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </a-modal>
      <!-- 修改物料的发料仓库-->
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        class="j-table-force-nowrap"
        :scroll="{ x: true }"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: 'radio' }"
        :customRow="clickThenSelect"
        @change="handleTableChange"
      >
        <template slot="status" slot-scope="text">
          <a-tag :color="text === '未审核' ? 'red' : 'green'">{{ text }}</a-tag>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)" v-if="record.status === 0">编辑</a>
          <a-divider style="background-color: black" type="vertical" v-if="record.status === 0" />
          <a @click="handleDelete(record.id)" v-if="record.status === 0">删除</a>
          <a-divider style="background-color: black" type="vertical" v-if="record.status === 0" />
          <a @click="handleUndoCheck(record)" v-if="record.status === 1">反审核</a>
          <a @click="handleCheck(record)" v-else>审核</a>
        </span>
      </a-table>
    </div>

    <a-tabs defaultActiveKey="1">
      <a-tab-pane tab="BOM单物料" key="1">
        <HfBomDetailList :mainId="selectedMainId" ref="DetailList" />
      </a-tab-pane>
    </a-tabs>

    <hfBom-modal ref="modalForm" @ok="modalFormOk"></hfBom-modal>
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import HfBomModal from './modules/HfBomModal'
import { downFile, getAction, postAction } from '@/api/manage'
import HfBomDetailList from './HfBomDetailList'
import '@/assets/less/TableExpand.less'
import dayjs from 'dayjs'

export default {
  name: 'HfBomCheckList',
  mixins: [JeecgListMixin],
  components: {
    HfBomDetailList,
    HfBomModal
  },
  data() {
    return {
      description: 'bom单信息管理页面',
      warehouseEditModal: {
        visible: false,
        title: '修改物料的发料仓库',
        confirmLoading: false
      },
      queryParam: {
        status: 1
      },
      // 表头
      columns: [
        {
          title: 'BOM编号',
          align: 'center',
          dataIndex: 'bomCode'
        },
        {
          title: '工序',
          align: 'center',
          dataIndex: 'step_dictText'
        },
        {
          title: '物料代码',
          align: 'center',
          dataIndex: 'materialCode'
        },
        {
          title: '物料名称',
          align: 'center',
          dataIndex: 'materialName'
        },
        {
          title: '规格型号',
          align: 'center',
          dataIndex: 'sap'
        },
        // {
        //   title: '物料属性',
        //   align: 'center',
        //   dataIndex: 'materialProperty_dictText'
        // },
        {
          title: '单位',
          align: 'center',
          dataIndex: 'unit_dictText'
        },
        {
          title: '标准用量',
          align: 'center',
          dataIndex: 'number'
        },
        {
          title: '标准消耗工时',
          align: 'center',
          dataIndex: 'manHour'
        },
        {
          title: '收料仓库',
          align: 'center',
          dataIndex: 'warehouse_dictText'
        },
        {
          title: '审核状态',
          align: 'center',
          dataIndex: 'status_dictText',
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '制单',
          align: 'center',
          dataIndex: 'operator'
        },
        {
          title: '审核员',
          align: 'center',
          dataIndex: 'checker'
        },
        {
          title: '审核时间',
          align: 'center',
          dataIndex: 'checkTime'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/material/hfBom/list',
        delete: '/material/hfBom/delete',
        deleteBatch: '/material/hfBom/deleteBatch',
        exportXlsUrl: '/material/hfBom/exportXls',
        importExcelUrl: 'material/hfBom/importExcel',
        check: '/material/hfBom/check',
        undoCheck: '/material/hfBom/undoCheck'
      },
      dictOptions: {},
      /* 分页参数 */
      ipagination: {
        current: 1,
        pageSize: 5,
        pageSizeOptions: ['5', '10', '50'],
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + ' 共' + total + '条'
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0
      },
      selectedMainId: '',
      superFieldList: []
      // disableMixinCreated:true
    }
  },
  created() {},
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    editMaterialWarehouse() {
      if (!this.warehouseEditModal.materialType || !this.warehouseEditModal.warehouse) {
        this.$message.warning('请选择物料类型和发料仓库')
        return
      }
      this.warehouseEditModal.confirmLoading = true
      const form = new FormData()
      form.append('materialType', this.warehouseEditModal.materialType)
      form.append('warehouse', this.warehouseEditModal.warehouse)
      postAction('/material/hfBom/editMaterialWarehouse', form)
        .then(res => {
          if (res.success) {
            this.$message.success(res.message)
            this.warehouseEditModal.visible = false
            this.loadData()
          }
        })
        .finally(() => {
          this.warehouseEditModal.confirmLoading = false
        })
    },
    clickThenSelect(record) {
      return {
        on: {
          click: () => {
            this.onSelectChange(record.id.split(','), [record])
          }
        }
      }
    },
    onClearSelected() {
      this.selectedRowKeys = []
      this.selectionRows = []
      this.selectedMainId = ''
    },

    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedMainId = selectedRowKeys[0]
      this.selectedRowKeys = selectedRowKeys
      this.selectionRows = selectionRows
      this.$refs.DetailList.isChecked = true
    },
    loadData(arg) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      this.onClearSelected()
      var params = this.getQueryParams() //查询条件
      params['status'] = 1
      this.loading = true
      getAction(this.url.list, params).then(res => {
        if (res.success) {
          this.dataSource = res.result.records
          this.ipagination.total = res.result.total
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
        this.loading = false
      })
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
